/**
 * 简单测试320指令
 */

const WebSocket = require('ws');

async function test320Simple() {
    console.log('🚀 开始简单测试320指令...');

    return new Promise((resolve, reject) => {
        const ws = new WebSocket('ws://localhost:3000/user-service/websocket?t=a63a2224db3dead54b30d997bb14175c');

        ws.on('open', () => {
            console.log('✅ WebSocket连接成功');

            // 发送320指令
            const message = { c: 320 };
            console.log('📤 发送320指令:', JSON.stringify(message));
            ws.send(JSON.stringify(message));
        });

        ws.on('message', (data) => {
            try {
                const response = JSON.parse(data.toString());
                console.log('📥 收到响应:', JSON.stringify(response, null, 2));

                if (response.act_id === 320) {
                    if (response.code === 0) {
                        console.log('✅ 320指令执行成功');
                        console.log(`📊 上阵副将数量: ${response.list ? response.list.length : 0}`);

                        if (response.list && response.list.length > 0) {
                            console.log('📋 上阵副将列表:');
                            response.list.forEach((deputy, index) => {
                                const gender = deputy.sex === 1 ? '男' : '女';
                                console.log(`  ${index + 1}. ID:${deputy.id} 头像:${deputy.headImg} 职业:${deputy.roleType} 性别:${gender}`);
                            });
                        }
                    } else {
                        console.error('❌ 320指令执行失败:', response.msg);
                    }
                }

                ws.close();
                resolve(response);

            } catch (error) {
                console.error('❌ 解析响应失败:', error);
                ws.close();
                reject(error);
            }
        });

        ws.on('error', (error) => {
            console.error('❌ WebSocket错误:', error);
            reject(error);
        });

        ws.on('close', () => {
            console.log('🔌 WebSocket连接已关闭');
        });
    });
}

// 运行测试
if (require.main === module) {
    test320Simple()
        .then(() => {
            console.log('🎉 测试完成');
            process.exit(0);
        })
        .catch((error) => {
            console.error('💥 测试失败:', error);
            process.exit(1);
        });
}

module.exports = { test320Simple };